package pannel.product;

import utils.JDBCUtils;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductDao {
    private static final String TABLE_NAME = "product";

    // 分页查询
    public List<Product> pageQuery(String searchText, int page, int pageSize) throws SQLException {
        String sql = "SELECT * FROM " + TABLE_NAME;
        if (searchText != null && !searchText.trim().isEmpty()) {
            sql += " WHERE name LIKE ?";
        }
        sql += " LIMIT ? OFFSET ?";

        List<Object> params = new ArrayList<>();
        if (searchText != null && !searchText.trim().isEmpty()) {
            params.add("%" + searchText + "%");
        }
        params.add(pageSize);
        params.add((page - 1) * pageSize);

        ResultSet rs = (ResultSet) JDBCUtils.execute(sql, params.toArray());
        List<Product> products = new ArrayList<>();
        while (rs.next()) {
            products.add(mapResultSetToProduct(rs));
        }
        return products;
    }

    // 获取总记录数
    public int getTotalCount(String searchText) throws SQLException {
        String sql = "SELECT COUNT(*) as total FROM " + TABLE_NAME;
        if (searchText != null && !searchText.trim().isEmpty()) {
            sql += " WHERE name LIKE ?";
        }
        ResultSet rs = (ResultSet) JDBCUtils.execute(sql, 
            searchText != null && !searchText.trim().isEmpty() ? new Object[]{"%" + searchText + "%"} : new Object[]{});
        return rs.next() ? rs.getInt("total") : 0;
    }

    // 新增商品
    public boolean insert(Product product) throws SQLException {
        String sql = "INSERT INTO " + TABLE_NAME + " (name, price, stock, image_path) VALUES (?, ?, ?, ?)";
        return (int) JDBCUtils.execute(sql, 
            product.getName(), 
            product.getPrice(),
            product.getStock(),
            product.getImagePath()) > 0;
    }

    // 更新商品
    public boolean updateById(Product product) throws SQLException {
        String sql = "UPDATE " + TABLE_NAME + " SET name=?, price=?, stock=?, image_path=? WHERE id=?";
        return (int) JDBCUtils.execute(sql,
            product.getName(),
            product.getPrice(),
            product.getStock(),
            product.getImagePath(),
            product.getId()) > 0;
    }

    // 删除商品
    public boolean deleteById(Integer id) throws SQLException {
        String sql = "DELETE FROM " + TABLE_NAME + " WHERE id=?";
        return (int) JDBCUtils.execute(sql, id) > 0;
    }

    // 添加根据ID获取商品的方法
    public Product getById(Integer id) throws SQLException {
        String sql = "SELECT * FROM product WHERE id = ?";
        ResultSet rs = (ResultSet) JDBCUtils.execute(sql, id);
        if (rs.next()) {
            return Product.builder()
                    .id(rs.getInt("id"))
                    .name(rs.getString("name"))
                    .price(rs.getDouble("price"))
                    .stock(rs.getInt("stock"))
                    .imagePath(rs.getString("image_path"))
                    .build();
        }
        return null;
    }

    private Product mapResultSetToProduct(ResultSet rs) throws SQLException {
        return Product.builder()
                .id(rs.getInt("id"))
                .name(rs.getString("name"))
                .price(rs.getDouble("price"))
                .stock(rs.getInt("stock"))
                .imagePath(rs.getString("image_path"))
                .build();
    }
} 